#include "Subsystems/BlockBehaviors/SubsystemBottomSuckerBlockBehavior.h"
#include "Subsystems/SubsystemTerrain.h"
#include "Blocks/Block.hpp"
#include "Blocks/BottomSuckerBlock.h"
#include "Managers/BlocksManager.h"
#include "Components/ComponentHealth.h"
#include "Components/ComponentBody.h"
#include "Components/ComponentCreature.h"
#include "GameSingleton.h"


namespace PocketSurvival
{

    bool SubsystemBottomSuckerBlockBehavior::isSupport(int32_t value, int32_t face)
	{
		Block *block = GameSingleton::blocksManager->blocks[Terrain::ExtractContents(value)];
		if (block->IsCollidable)
		{
			return false == block->isFaceTransparent(CellFace::OppositeFace(face), value);
		}
		return false;
	}

    void SubsystemBottomSuckerBlockBehavior::onNeighborBlockChanged(int32_t x, int32_t y, int32_t z, int32_t neighborX, int32_t neighborY, int32_t neighborZ)
    {
		SubsystemInWaterBlockBehavior::onNeighborBlockChanged(x, y, z, neighborX, neighborY, neighborZ);
		int face = BottomSuckerBlock::GetFace(Terrain::ExtractData(subsystemTerrain->getCellValue(x, y, z)));
		Point3 point = CellFace::FaceToPoint3(CellFace::OppositeFace(face));
		int cellValue = subsystemTerrain->getCellValue(x + point.posX, y + point.posY, z + point.posZ);
		if (false == isSupport(cellValue, face))
		{
            subsystemTerrain->destroyCell(0, x, y, z, 0, false);
		}
    }

    void SubsystemBottomSuckerBlockBehavior::onCollide(const CellFace &cellFace, float velocity, ComponentBody *componentBody)
	{
		if (Terrain::ExtractContents(subsystemTerrain->getCellValue(cellFace.point.posX, cellFace.point.posY, cellFace.point.posZ)) == 226)
		{
            if(componentBody->componentCreature != nullptr)
            {
                componentBody->componentCreature->componentHealth->injure(0.01f * MathUtils::Abs(velocity), entt::null, false, "被海洋生物刺死了");;
            }
		}
	}

    static const std::string subsystemName = "BottomSuckerBlockBehavior";
    const std::string& SubsystemBottomSuckerBlockBehavior::getName() const
    {
        return subsystemName;
    }

} // namespace PocketSurvival

